<template>
  <el-dialog title="历史记录管理" :visible.sync="dialog" :before-close="closeHistory" top="5vh"
             width="45%">
    <div class="search-hisorty">
      <el-date-picker
        style="width: 350px"
        size="mini"
        v-model="time"
        type="datetimerange"
        range-separator="至"
        start-placeholder="开始时间"
        end-placeholder="结束时间">
      </el-date-picker>
      <el-button type="primary" icon="el-icon-search" @click="onclickQuery" class="btn" size="mini">查询</el-button>
      <el-button type="danger" icon="el-icon-delete" class="btn" @click="onclickDetele" size="mini">批量删除</el-button>
    </div>
    <el-table
      ref="multipleTable"
      :data="historyData"
      tooltip-effect="dark"
      height="380"
      style="width: 95%;margin:auto;"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        label="序号"
        type="index">
      </el-table-column>
      <el-table-column
        prop="status"
        label="状态">
        <template slot-scope="scope">
          <span class="not dzx" v-if="scope.row.status == 0">待执行</span>
          <span class="not zxz" v-else-if="scope.row.status == 1">执行中</span>
          <span class="not success" v-else-if="scope.row.status == 2">执行成功</span>
          <span class="not error" v-else-if="scope.row.status == 3">执行失败</span>
        </template>
      </el-table-column>
      <el-table-column
        label="时间">
        <template slot-scope="scope">{{scope.row.startTime | formatDate}}</template>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next"
        :total="page.total">
      </el-pagination>
    </div>
  </el-dialog>
</template>
<script>
  import { formatDateTime } from "@/libs/utils"
  export default {
    name:'taskHistoryManage',
    props:{
      dialog:{
        type:Boolean,
        default:false,
        required:true
      },
      taskInvokeId:{
        type:String,
        default: '',
        required: true
      }
    },
    data(){
      return {
        page: {
          pageSize: 10,
          pageNum: 1,
          flag: false,
          total: 0,
          startTime: null,
          endTime: null,
        },
        time: [],
        historyData: [
          {
            status: 0,
            time: '2019-09-09 00:00:00'
          }
        ]
      }
    },
    filters: {
      formatDate(time) {
        let date = new Date(time);
        return formatDateTime(date, 'yyyy-MM-dd hh:mm:ss');
      }
    },
    created(){
      this.onloadHistroyList()
    },
    methods:{
      //关闭历史管理弹窗
      closeHistory(){
        this.$emit('closeHistoryBox')
      },
      //点击查询
      onclickQuery(){
        this.page.startTime = this.time[0] || null;
        this.page.endTime = this.time[1] || null;
        this.onloadHistroyList();
      },
      //批量删除
      onclickDetele(){
        if(this.multipleSelection){
          let arr = []
          this.multipleSelection.forEach((item)=>{
            arr.push(item.recordId)
          })
          this.$http.delete('/taskInvoke/record/delete',{data:arr})
            .then((res)=>{
              this.onloadHistroyList()
              this.$message({message: '删除成功',type: 'success'});
            })
        }else{
          this.$message({message: '请选择删除项',type: 'warning'});
        }
      },
      //获取历史详情列表
      onloadHistroyList(){
        this.page.taskInvokeId = this.taskInvokeId
        this.$http.post('/taskInvoke/getHistoryInvokeDetail',this.page)
          .then((res)=>{
            this.historyData=res.list
            this.page.total = res.total
          })
      },
      //每页显示条数
      handleSizeChange(val){
        this.page.pageSize = val
        this.onloadHistroyList()
      },
      //当前页码
      handleCurrentChange(val){
        this.page.pageNum = val
        this.onloadHistroyList()
      },
      handleSelectionChange(val){
        this.multipleSelection = val;
      }
    }
  }
</script>
<style lang="less" scoped>
  .search-hisorty{padding:10px 20px;}
  .btn{margin:10px 0;margin-left: 10px}
  .page{padding:10px 20px;}
  .not{position: relative;padding-left: 10px;}
  .not:before{position: absolute;content: "";width: 6px;height: 6px;border-radius: 50%;left:0;top:7px;}
  .not.error,.steps-error{color: #f04134}
  .not.error:before{background: #f04134}
  .not.success,.steps-success{color: #108de9}
  .not.success:before{background: #108de9}
  .not.zxz,.steps-zxz{color: #669900}
  .not.zxz:before{background: #669900}
  .not.dzx,.steps-dzx{color: #cccccc}
  .not.dzx:before{background: #cccccc}
</style>
